import { Base_DataItemDetailServiceProxy } from '@shared/service-proxies/stock-service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Injector, OnInit, Component, ViewChild } from '@angular/core';
import { Table } from 'primeng/table';
import { Paginator, TreeNode, LazyLoadEvent } from 'primeng/primeng';
import { IBasicDataItemInfo } from './basic-dataitem-info';
import { finalize } from 'rxjs/operators';
import { CreateOrEditDataItemDetailModalComponent } from './create-or-edit-dataitem-detail-modal.component';

@Component({
    selector: 'dataItemDetails',
    templateUrl: './dataitem-details.component.html'
})
export class DataItemDetailsComponent extends AppComponentBase implements OnInit {
    @ViewChild('createOrEditDataItemDetailModal')
    createOrEditDataItemDetailModal: CreateOrEditDataItemDetailModalComponent;
    constructor(injector: Injector, private _baseItemsDetailService: Base_DataItemDetailServiceProxy) {
        super(injector);
    }
    private _basicDataItemInfo: IBasicDataItemInfo = null;
    get basicDataItemInfo(): IBasicDataItemInfo {
        return this._basicDataItemInfo;
    }

    set basicDataItemInfo(ou: IBasicDataItemInfo) {
        if (this._basicDataItemInfo === ou) {
            return;
        }

        this._basicDataItemInfo = ou;
        this.getDataItemDetails();
    }
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    ngOnInit(): void {}

    getDataItemDetails(event?: LazyLoadEvent) {
        if (!this._basicDataItemInfo) {
            return;
        }

        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);

            return;
        }

        this.primengTableHelper.showLoadingIndicator();
        this._baseItemsDetailService
            .getDataItemDetails(
                this._basicDataItemInfo.id,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator()))
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    deleteUser(id: number) {
        this.message.confirm('确认删除该条数据吗？', isConfirmed => {
            if (isConfirmed) {
                this._baseItemsDetailService.deleteDataItemDetail(id).subscribe(() => {
                    this.notify.success(this.l('SuccessfullyDeleted'));
                    this.getDataItemDetails();
                });
            }
        });
    }

    addItemDetail(parentId?: number): void {
        if (this._basicDataItemInfo == null) {
            this.notify.error(this.l('请选择字典分类'));
        } else {
            this.createOrEditDataItemDetailModal.show({
                parentId: this._basicDataItemInfo.id
            });
        }
    }
}
